package ${package.Service}.impl;

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cly.properties.SystemProperties;
import com.cly.result.JsonRequest;
import exception.result.ServiceException;
import exenum.result.RequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* ${table.comment}业务层实现
* @author ${author}
* <p>
* ${date}
*/
@Service
public class ${table.serviceImplName} extends ServiceImpl<${table.mapperName},${entity}> implements ${table.serviceName}{

    /**
    * 导入系统配置
    */
    @Resource
    private SystemProperties systemProperties;

    /**
    *  根据条件查询${table.comment}信息
    *  @param ${table.entityPath} 条件
    *  @return JSON
    */
    @Override
    @Cacheable(value = "${table.entityPath}_list", key = "#root.args")
    public JsonRequest<List<${entity}>> look${entity}(Integer current, Integer size,${entity} ${table.entityPath}){
        IPage<${entity}> ${table.entityPath}IPage = baseMapper.select${entity}Pages(new Page<>(current, size), ${table.entityPath});
        //判断是否有${table.comment}信息
        List<${entity}> ${table.entityPath}List = ${table.entityPath}IPage.getRecords();
        if (${table.entityPath}List.isEmpty()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        //传入分页
        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstants.PAGE_KEY_NAME, ${table.entityPath}IPage.getTotal());
        return JsonRequest.success(${table.entityPath}List, map);
    }

    /**
    *  新增一条${table.comment}信息
    *  @param ${table.entityPath} ${table.comment}信息
    *  @return JSON
    */
    @Override
    @CacheEvict(value = "${table.entityPath}_list", allEntries = true)
    public JsonRequest<Integer> addOne${entity}(${entity} ${table.entityPath}){
        //判断${table.comment}是否存在
        Integer size = baseMapper.count${entity}ByCondition(${table.entityPath});
        if (size > 0) {
            throw new ServiceException(RequestException.DATA_EXISTS_ERROR);
        }
        //插入${table.comment}信息
        int column = baseMapper.insert(${table.entityPath});
        if (column < 1) {
            throw new ServiceException(RequestException.INSERT_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
    *  修改一条${table.comment}信息
    *  @param ${table.entityPath} ${table.comment}信息
    *  @return JSON
    */
    @Override
    @CacheEvict(value = "${table.entityPath}_list", allEntries = true)
    public JsonRequest<Integer> updateOne${entity}(${entity} ${table.entityPath}){
        ${entity} ${table.entityPath}Object = baseMapper.selectById(${table.entityPath}.getId());
        if (Objects.isNull(${table.entityPath}Object)) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        //判断待修改的${table.comment}信息是否已经存在
        Integer size = baseMapper.count${entity}ByCondition(${table.entityPath});
        if (size > 0) {
            throw new ServiceException(RequestException.DATA_EXISTS_ERROR);
        }
        int column = baseMapper.updateById(${table.entityPath});
        if (column < 1) {
            throw new ServiceException(RequestException.UPDATE_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
    *  批量删除${table.comment}信息
    *  @param ids ${table.comment}id列表
    *  @return JSON
    */
    @Override
    @CacheEvict(value = "${table.entityPath}_list", allEntries = true)
    public JsonRequest<Integer> delete${entity}s(List<Long> ids){
        //查询${table.comment}信息
        List<${entity}> ${table.entityPath}List = baseMapper.selectBatchIds(ids);
        //判断待删除的${table.comment}信息是否存在
        if (${table.entityPath}List.size() != ids.size()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        int column = baseMapper.deleteByIds(ids);
        if (column < 1) {
            throw new ServiceException(RequestException.DELETE_ERROR);
        }
        return JsonRequest.success(column);
    }

}